package com.xxxx.manager.controller;


import com.xxxx.common.result.BaseResult;
import com.xxxx.manager.pojo.GoodsCategory;
import com.xxxx.manager.service.impl.GoodsCategoryServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ppl
 * @since 2021-01-01
 */
@Controller
@RequestMapping("/goodsCategory")
public class GoodsCategoryController {

    @Autowired
    private GoodsCategoryServiceImpl goodsCategoryService;

    /**
     * 跳转 商品分类-新增页
     * @return
     */
    @RequestMapping("/addPage")
    public String categoryAddPage(Model model){
        //获取商品分类的顶级数据
        List<GoodsCategory> gcList = goodsCategoryService.selectCategoryParentId(0);
        model.addAttribute("gcList",gcList);
        List<Integer> allGroup = goodsCategoryService.getAllGroup();
        model.addAttribute("catGroupList",allGroup);
        model.addAttribute("catGroupSize",allGroup.size());
        return "goods-manager/categoryList/category-add";
    }

    /**
     * 商品分类 pId list
     * @param parentId
     * @return
     */
    @RequestMapping("/{parentId}")
    @ResponseBody
    public List<GoodsCategory> selectCategoryList(@PathVariable Integer parentId){
        return goodsCategoryService.selectCategoryParentId(parentId);
    }

    @RequestMapping("/addCategory")
    @ResponseBody
    public BaseResult saveCategory(GoodsCategory goodsCategory){
        System.out.println(goodsCategory);
        goodsCategoryService.saveCategory(goodsCategory);
        return BaseResult.success();
    }

    @RequestMapping("/updatePage")
    public String updateCategoryPage(){
        return "goods-manager/categoryList/category-update";
    }

}
